package com.zh.chat;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

/**
 * gRPC启动
 * @author Zhou
 * @date 14:29 2025/9/11
 */
public class AppGrpc {
    private static Server server;

    public static void main(String[] args) throws IOException, InterruptedException {
        server = ServerBuilder.forPort(9999)
                .addService(new ChatServiceImpl())
                .build();
        server.start();
        System.out.printf("Server started, listening on port %d%n", 9999);
        server.awaitTermination();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.err.println("*** shutting down gRPC server since JVM is shutting down");
            try {
                server.shutdown().awaitTermination(30, java.util.concurrent.TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace(System.err);
            }
            System.err.println("*** server shut down");
        }));
    }
}
